import os, os.path, sys, math, csv
import wx, wx.grid
import pr, prload

class app(wx.App):
  def __init__(self, redirect=False, filename=None):
    wx.App.__init__(self, redirect, filename)
    self.frame = wx.Frame(None, wx.ID_ANY, title="Scottish Parliament Sim")
    self.panel = wx.Panel(self.frame, wx.ID_ANY)
    self.grid = None
    self.isregion = False
    #Main sizer
    self.sizer = wx.BoxSizer(wx.VERTICAL)
    #self.sizer.Fit(self.panel)
    
    #Choice list
    self.region_sizer = wx.BoxSizer(wx.HORIZONTAL)
    self.sizer.Add(self.region_sizer)
    self.regions = []
    
    curdir = os.path.join(os.getcwd(),'index.csv') #This becomes a pain if you try to run it from within an ide - launch it by the command line or double clicking
    
    index = prload.load_index(curdir) #This is a holding value - should be able to select a directory.
    for i in index:
      self.regions.append(i)
    self.region = self.regions[0]
    #self.regions.append(prload.load_file("C:\\Users\\ruaraidh\\Documents\\Downloads\\Python\\GlasgowRegion.csv"))
    self.consts = self.region.constituencies
    self.con_names = []
    self.reg_names = []
    for i in self.consts:
      self.con_names.append(i.name)
      
    for i in self.regions:
      self.reg_names.append(i.name)
      
    self.con_names.append(self.region.name)
    self.regionchoice = wx.Choice(self.panel, -1, wx.DefaultPosition, choices = self.reg_names)
    self.region_sizer.Add(self.regionchoice)
    self.Bind(wx.EVT_CHOICE, self.display_region_results, self.regionchoice)
    self.choose = wx.Choice(self.panel, -1, wx.DefaultPosition, choices = self.con_names)
    self.Bind(wx.EVT_CHOICE, self.display_results, self.choose)
    self.regionchoice.SetSelection(0)
    self.region_sizer.Add(self.choose, 0, wx.EXPAND, 5)
    self.region_sizer.Fit(self.panel)
    self.sizer.Fit(self.frame)
    
    self.panel.SetSizer(self.sizer)
    self.panel.Layout()
    self.frame.SetAutoLayout = True
    self.frame.Layout()
    self.frame.Show()
    
  #def get_regions(self, filename):
  #  country = prload.load_index(filename)
    
  def display_region_results(self, event):
    for i in self.regions:
      if i.name == self.reg_names[self.regionchoice.GetSelection()]:
        self.region = i
    self.consts = self.region.constituencies
    self.con_names = []
    for i in self.consts:
      self.con_names.append(i.name)
      #print i.name
    self.con_names.append(self.region.name)
    
    self.choose.Clear()
    self.choose.AppendItems(self.con_names)
    
    self.choose.SetSelection(0)
    self.display_results(0)
    
  def display_results(self, event):
    try:
      self.grid.Destroy()
      self.fromchoice.Destroy()
      self.tochoice.Destroy()
      self.swinglab.Destroy()
      self.swingtextbox.Destroy()
      self.swingbtn.Destroy()
    except:
      pass

    if self.con_names[self.choose.GetSelection()] == self.region.name:
      i = self.region
      #print i.name
      self.isregion = True
    else:
      i = self.consts[self.choose.GetSelection()]
      self.isregion = False
      #print i.name
      
    self.grid = wx.grid.Grid(self.panel)
    self.grid.CreateGrid(len(i.parties)+1, 6)
    x = wx.BoxSizer(wx.HORIZONTAL)
    self.sizer.Add(self.grid, 1, wx.EXPAND, 5)
    self.sizer.Fit(self.frame)
    self.grid.SetRowLabelSize(0)
    self.grid.SetColLabelSize(0)
    self.set_grid_results(i)
    if self.isregion == True:
      self.setregionresults(i)
    
    parties = []
    for k in i.parties:
      parties.append(k.name)
      
    self.swinglab = wx.StaticText(self.panel, wx.ID_ANY, "Swing from... to")
    self.fromchoice = wx.Choice(self.panel, -1, wx.DefaultPosition, choices = parties)
    self.tochoice = wx.Choice(self.panel, -1, wx.DefaultPosition, choices = parties)
    self.swingtextbox = wx.TextCtrl(self.panel, wx.ID_ANY, "%")
    self.swingbtn = wx.Button(self.panel, wx.ID_ANY, "Swing!")
    
    self.Bind(wx.EVT_BUTTON, self.swing, self.swingbtn)
    
    x.Add(self.swinglab,1,wx.ALIGN_BOTTOM|wx.ALIGN_LEFT|wx.ALL)
    x.Add(self.fromchoice, 1, wx.ALIGN_BOTTOM|wx.ALL)
    x.Add(self.tochoice, 1, wx.ALIGN_BOTTOM|wx.ALL)
    x.Add(self.swingtextbox, 1, wx.ALIGN_BOTTOM|wx.ALL)
    x.Add(self.swingbtn, 1, wx.ALIGN_BOTTOM|wx.ALIGN_RIGHT|wx.ALL)
    self.sizer.Add(x, 0, wx.CENTER, 5)
    self.sizer.Fit(self.frame)
    
    self.panel.Layout()


  def set_grid_results(self,i):
    self.grid.SetCellValue(0,0,"Party")
    self.grid.SetCellValue(0,1,"Votes")
    self.grid.SetCellValue(0,2,"%")
    self.grid.SetCellValue(0,3,"")
    
    y = 1
    for j in i.parties:
      self.grid.SetCellValue(y,0,j.name)
      self.grid.SetCellValue(y,1,str(j.votes))
      self.grid.SetCellValue(y,2,str(round(float(j.votes)/float(i.totalvotes)*100.00,2)))     
      y += 1
      
  def setregionresults(self,i):
    self.grid.SetCellValue(0,3,"Elected")
    self.grid.SetCellValue(0,4,"Constituency")
    self.grid.SetCellValue(0,5,"Winner")
    y = 1
    for j in i.get_elected():
      self.grid.SetCellValue(y,3,j.name)
      y += 1
    y = 1
    for j in i.constituencies:
      self.grid.SetCellValue(y,4,j.name)
      self.grid.SetCellValue(y,5,j.get_elected().name)
      y+=1
      
  def swing(self,event):
    if self.con_names[self.choose.GetSelection()] == self.region.name:
      i = self.region
    else:
      i = self.consts[self.choose.GetSelection()]
    i.uniform_swing(int(self.swingtextbox.GetValue()), self.fromchoice.GetStringSelection(),self.tochoice.GetStringSelection())
    if self.isregion == True:
      i.constituency_uniform_swing(int(self.swingtextbox.GetValue()), self.fromchoice.GetStringSelection(),self.tochoice.GetStringSelection())
    self.set_grid_results(i)
    if self.isregion == True:
      self.setregionresults(i)
      
    
if __name__ == "__main__":
  a = app()
  a.MainLoop()